package com.atguigu.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


import com.atguigu.gulimall.product.entity.BrandEntity;
import com.atguigu.gulimall.product.vo.Brandvo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.entity.CategoryBrandRelationEntity;
import com.atguigu.gulimall.product.service.CategoryBrandRelationService;
import com.atguigu.common.no.utils.PageUtils;
import com.atguigu.common.no.utils.R;



/**
 * 品牌分类关联
 *
 * @author xuejian
 * @email 160994754@qq.com
 * @date 2020-06-27 11:40:23
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 获取当前索引品牌的列表
     */

    @GetMapping("/catelog/list")
    public R listbrand(@RequestParam Long  brandId){

        List<CategoryBrandRelationEntity> data = categoryBrandRelationService.list(
                new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId)
        );

        return R.ok().put("data", data);
    }

    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/brands/list")
    public R brandslist(@RequestParam ("catId") Long catId){

       List<BrandEntity> brandvoList= categoryBrandRelationService.getByBrabdsBYid(catId);

      List<Brandvo> collect= brandvoList.stream().map(item -> {
            Brandvo brandvo = new Brandvo();
            brandvo.setBrandId(item.getBrandId());
            brandvo.setBrandName(item.getName());
            return brandvo;
        }).collect(Collectors.toList());

        return R.ok().put("data", collect);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
   
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){

		//categoryBrandRelationService.save(categoryBrandRelation);
        categoryBrandRelationService.saveDetile(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")

    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
